﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using Refit;

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;


using MyETFData.Model;
using MyETFData.Service;


namespace EastMoneyReader
{
    internal class Program
    {



        static IETFService _ETFService = RestService.For<IETFService>(ConfigurationManager.AppSettings["Postgrest:Server"]);


        static ChromeOptions opt = new ChromeOptions();
        static ChromeDriver driver = new ChromeDriver(ConfigurationManager.AppSettings["ChromeDriver:Path"], opt);




        static void Main(string[] args)
        {
            List<ETFInfo> etfDataList = _ETFService.GetETFInfosAsync().Result;

            foreach (var etf in etfDataList)
            {
                GetData(etf.etf_code);

                Thread.Sleep(5678);
            }

            driver.Close();

        }




        static void GetData(string code)
        {
            Console.WriteLine(code);



            driver.Navigate().GoToUrl($"http://quote.eastmoney.com/{code}.html");



            // 最新价的 div.
            var currentPriceDiv = driver.FindElementByClassName("zxj");
            string priceText = currentPriceDiv.Text;



            // 日期.
            var dateDiv = driver.FindElementByClassName("quote_title_time");
            string dateText = dateDiv.Text;

            // （2022-02-07 15:46:07）
            dateText = dateText.Substring(1, 10);



            var tab = driver.FindElementByClassName("brief_info_c");

            string tabText = tab.Text;
/*
今开: 3.082 最高: 3.111 涨停: 3.340 换手: 3.72% 成交量: 740.4万 振幅: 1.22% 外盘: 327.1万
昨收: 3.036 最低: 3.074 跌停: 2.732 量比: 0.83 成交额: 22.91亿 均价: 3.094 内盘: 413.3万
*/


            string highStr = GetValue(tabText, "最高: ", "涨停");
            string openStr = GetValue(tabText, "今开: ", "最高");
            string lowStr = GetValue(tabText, "最低: ", "跌停");
            string volumeStr = GetValue(tabText, "成交额: ", "均价");



            DayLineRequest request = new DayLineRequest();
            request.p_code = code;

            request.p_date = dateText;

            request.p_open = Convert.ToDecimal(openStr.Trim());
            request.p_close = Convert.ToDecimal(priceText);

            request.p_highest = Convert.ToDecimal(highStr.Trim());
            request.p_lowest = Convert.ToDecimal(lowStr.Trim());


            volumeStr = volumeStr.Trim();
            if (volumeStr.EndsWith("亿"))
            {
                volumeStr = volumeStr.Replace("亿", "");
                request.p_volume = Convert.ToDecimal(volumeStr) * 100000000;
            }
            else if (volumeStr.EndsWith("万"))
            {
                volumeStr = volumeStr.Replace("万", "");
                request.p_volume = Convert.ToDecimal(volumeStr) * 10000;
            }



            ServiceResult result = _ETFService.InsertDayLineAsync(request).Result;

            Console.WriteLine($"Finish! Result = {result}");

        }



        /// <summary>
        /// 获取数值.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="nextTitle"></param>
        /// <returns></returns>
        private static string GetValue(string source, string title, string nextTitle)
        {
            int startIndex = source.IndexOf(title);
            int nextIndex = source.IndexOf(nextTitle);

            string result = source.Substring(startIndex + title.Length,
                nextIndex - (startIndex + title.Length));

            return result;
        }

    }

}
